﻿using System.Windows.Forms;

namespace Latin_Vocabulary_Indicator
{
    public partial class resultbox : Form
    {
        public ListViewItem[] calledDetail;
        public mainproj _caller;
        private void addItem(sbyte groupIndex, string name_, string content_)
        {
            ListViewItem temp = new ListViewItem();
            temp.Group = resultList.Groups[groupIndex];
            temp.Text = name_;
            temp.SubItems.Add( content_);
            resultList.Items.Add(temp);
        }

        public resultbox(mainproj caller,mainproj.testType testype)
        {
            InitializeComponent();
            _caller = caller;
            resultList.Items.Clear();
            title.Text = "Result: "+caller.title.Text;
            if(testype== mainproj.testType.review)
            {
                addItem(0, "Total", caller.bar.Maximum.ToString());
                addItem(0, "Rotation", ((short)((caller.taskLeaked+caller.answerTime - (caller.bar.Maximum * (int)System.Math.Ceiling(caller.options[0] / (float)caller.options[1]))) / (caller.options[2] / (float)caller.options[0])  / caller.bar.Maximum *100 )).ToString());
            }
            else if (testype == mainproj.testType.test)
            {
                short correct = 0;short pos_mistaken = 0;short accent_mistaken = 0;short spelling_mistaken = 0;short skipped = 0;
                calledDetail = caller.detail;
                foreach(ListViewItem sub in calledDetail)
                {
                    if (sub.Tag.ToString() == "correct") correct++;
                    else if (sub.Tag.ToString() == "pos mistaken") pos_mistaken++;
                    else if (sub.Tag.ToString() == "accent mistaken") accent_mistaken++;
                    else if (sub.Tag.ToString() == "incorrect") spelling_mistaken++;
                    else if (sub.Tag.ToString() == "skipped") skipped++;
                    else throw new System.Exception("Not occured: " + sub.Tag.ToString());
                }
                addItem(0, "Accuracy", (System.Math.Round( (float)correct / calledDetail.Length * 100,2)).ToString()+"%");
                addItem(0, "Condition", correct + "/" + calledDetail.Length);
                addItem(1, "Correct", correct.ToString());
                addItem(1, "Pos mistaken", pos_mistaken.ToString());
                addItem(1, "Accent mistaken", accent_mistaken.ToString());
                addItem(1, "Spelling mistaken", spelling_mistaken.ToString());
                addItem(1, "Skipped", skipped.ToString());
                detail.Enabled = true;
            }
        }

        private void detail_Click(object sender, System.EventArgs e)
        {
            new resDetail(this).Show();
            Hide();
        }

        private void yes_Click(object sender, System.EventArgs e)
        {
            Close();
        }

        private void resultbox_FormClosing(object sender, FormClosingEventArgs e)
        {
            fileOperator.totalQuit = true;
            Application.Exit();
        }

        private void save_Click(object sender, System.EventArgs e)
        {
            Enabled = false;
            new profile_saver(this).Show();
        }
    }
}
